<?
//<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
require ("s_inc.php");
$rows = 20;
$page	  = $_REQUEST['page']?$_REQUEST['page']:1;
//權限
admin_main::pageChk('cats_set');
?>
<?php require("s_head.tpl.php"); ?>
<?
$db_goods = &new db($scfg['db']['goods']);
$table 	  = 'cats_shop';
if($_POST)
{
	$act = $_POST['act'];
	if($act == 'modify')
	{
		//print_r($_POST);
		//更新
		$csid = $_POST['csid'];
		$name = $_POST['name'];
		$taxis = $_POST['taxis'];
		$count = $_POST['count'];
		$note = $_POST['note'];
		
		if(is_array($csid))
		{
			foreach($csid as $k=>$v)
			{
				$bind = array(
					'name'=>$name[$k],
					'taxis'=>$taxis[$k],
					'count'=>$count[$k],
					'note'=>$note[$k]
				);
				$db_goods->update($table,$bind," csid = ? ",$v);			
			}
		}
		//刪除
		$del = $_POST['del'];
		if(is_array($del))
		{
			$db_goods->delete($table," csid in (?) ",$del);
		}
	}elseif($act == 'add')
	{
		$name = $_POST['name'];
		$taxis = $_POST['taxis'];
		$note = $_POST['note'];
		$pcsid = $_POST['pcsid'];
		if($name)
		{
			$bind = array(
				'name'=>$name,
				'taxis'=>$taxis,
				'pcsid'=>$pcsid,
				'note'=>$note,				
			);
			$id = $db_goods->insert($table,$bind);
			if($id)
			{
				echo g::msg("添加成功! ");
			}else
			{
				echo g::msg("失败,请重试! ");
			}
		}
	}
}
$rs = $db_goods->dataArray("select * from {$table} order by pcsid,taxis");
$p  = array();
foreach($rs as $v)
{
	if($v['pcsid'] == '0')
	{
		$p[$v['csid']]['name'] = $v;
	}else
	{
		$p[$v['pcsid']]['sub'][] = $v;
	}
}
//print_r($p);
?>
<form method="post">
<table width="100%"  border="0" cellpadding="4" cellspacing="1" class="tableoutline">
	<tr>
	<td colspan="5" class="tbhead">商品類別管理</td>
  </tr>
  <tr class="_data_" style="font-weight:bold;">
	<td width="3%" align="right" style="color:#FF0000;">刪?</td>
	<td width="50%" align="left">分類名稱</td>
    <td width="17%" align="left">排序</td>
    <td width="12%" align="left">商舖數量</td>
    <td width="18%" align="left">備注</td>
  </tr>
  <?
  $option = '';
  foreach($p as $k=>&$v)
  {
  	 $rs = &$v['name'];
	 $option .='<option value="'.$rs['csid'].'">'.$rs['name'].'</option>';
	 //print_r( $rs);
  ?>
  <tr style="font-weight:bold; background-color:#FFCCCC;">
  	<input name="csid[<?=$rs['csid']?>]" type="hidden" value="<?=$rs['csid']?>" />
	<td width="3%" align="right"><input type="checkbox" name="del[]" value="<?=$rs['csid']?>" /></td>
	<td align="left"><input name="name[<?=$rs['csid']?>]" type="text" value="<?=$rs['name']?>" size="32" maxlength="32" />
	分類</td>
    <td align="left" colspan="3"><input name="taxis[<?=$rs['csid']?>]" type="text" value="<?=$rs['taxis']?>" size="2" maxlength="2" /></td>
    <input name="count[<?=$rs['csid']?>]" type="hidden" value="<?=$rs['count']?>" />
    <input name="note[<?=$rs['csid']?>]"  type="hidden" value="<?=$rs['note']?>" />
  </tr>
  <?
  	if(is_array($v['sub']))
	{
		foreach($v['sub'] as &$v)
		{
			$rs = $v;
			?>
			<tr class="_data_">
			<input name="csid[<?=$rs['csid']?>]" type="hidden" value="<?=$rs['csid']?>" />
			<td width="3%" align="right"><input type="checkbox" name="del[]" value="<?=$rs['csid']?>" /></td>
			<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;<input name="name[<?=$rs['csid']?>]" type="text" value="<?=$rs['name']?>" size="32" maxlength="32" /></td>
			<td align="left"><input name="taxis[<?=$rs['csid']?>]" type="text" value="<?=$rs['taxis']?>" size="2" maxlength="2" /></td>
			<td align="left"><input name="count[<?=$rs['csid']?>]" type="text" value="<?=$rs['count']?>" size="6" maxlength="6" /></td>
			<td align="left"><input name="note[<?=$rs['csid']?>]" type="text" value="<?=$rs['note']?>" /></td>
		  </tr>
			<?
		}
	}
  }
  ?>
 <tr class="_data_">
	<td>&nbsp;</td>
	<td colspan="4"><input name="Submit" type="submit" class="bginput" value="  送 出 " onClick="adminAdmin_chk()">
	<input name="Submit" type="reset" class="bginput" value=" 重 設 ">
	<input name="act" type="hidden" id="act" value="modify"></td>
  </tr>

</table>
  </form>
  
 <hr />
<form name="addcats" method="post" onsubmit="return chk(this)">
<table width="100%" border="0" cellpadding="4" cellspacing="1" class="tableoutline">
        <tr>
        <td colspan="2" class="tbhead">商品類別管理</td>
      </tr>
	  <tr class="_data_">
		<td align="right">分類名稱：</td>
		<td align="left"> 
		 <input name="name" type="text" id="name" size="20" maxlength="32"></td>
	  </tr>
	   <tr class="_data_">
        <td width="14%" align="right">父级：        </td>
        <td width="86%" align="left"><select name="pcsid" id="pcsid">
          <option value="0">0</option>
		  <?=$option?>
        </select>
        [选&quot;0&quot;时,新建一个父级]</td>
	   </tr>
	   <script type="text/javascript">
	   		var pcsid = '<?=$pcsid?>';
	   		if(pcsid)document.addcats.pcsid.value=pcsid;
	   </script>
      <tr class="_data_">
        <td width="14%" align="right">排序：        </td>
        <td width="86%" align="left"><input name="taxis" type="text" id="taxis" size="10" maxlength="10" value="0"></td>
      </tr>
      <tr class="_data_">
        <td align="right" valign="top">備註：</td>
        <td align="left">        <textarea name="note" cols="50" rows="3" id="note"></textarea></td>
      </tr>
      <tr class="_data_">
        <td>&nbsp;</td>
        <td><input type="submit" class="bginput" value="送出">
        <input type="reset" class="bginput" value="重設">
        <input name="act" type="hidden" id="act" value="add"></td>
      </tr>
</table>
</form>

<script language="JavaScript" type="text/JavaScript">
<!--
var chk = function(o)
{
	if(XypFrom.chk(o.name,'','请输入分類名稱!'))
	{
		return false;
	}else if(XypFrom.chk(o.taxis,'nums'))
	{
		return false;
	}else
	{
		return true;
	}
};
//---->
</script>
<? include("s_copyRight.tpl.php"); ?>